/**
 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package pies.chat.model;

import com.liferay.portal.kernel.bean.AutoEscapeBeanHandler;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ProxyUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.model.BaseModel;
import com.liferay.portal.model.impl.BaseModelImpl;

import pies.chat.service.ClpSerializer;
import pies.chat.service.DeviceLocalServiceUtil;

import java.io.Serializable;

import java.lang.reflect.Method;

import java.util.HashMap;
import java.util.Map;

/**
 * @author HoangAnh
 */
public class DeviceClp extends BaseModelImpl<Device> implements Device {
	public DeviceClp() {
	}

	@Override
	public Class<?> getModelClass() {
		return Device.class;
	}

	@Override
	public String getModelClassName() {
		return Device.class.getName();
	}

	@Override
	public long getPrimaryKey() {
		return _deviceId;
	}

	@Override
	public void setPrimaryKey(long primaryKey) {
		setDeviceId(primaryKey);
	}

	@Override
	public Serializable getPrimaryKeyObj() {
		return _deviceId;
	}

	@Override
	public void setPrimaryKeyObj(Serializable primaryKeyObj) {
		setPrimaryKey(((Long)primaryKeyObj).longValue());
	}

	@Override
	public Map<String, Object> getModelAttributes() {
		Map<String, Object> attributes = new HashMap<String, Object>();

		attributes.put("deviceId", getDeviceId());
		attributes.put("deviceToken", getDeviceToken());
		attributes.put("deviceName", getDeviceName());

		return attributes;
	}

	@Override
	public void setModelAttributes(Map<String, Object> attributes) {
		Long deviceId = (Long)attributes.get("deviceId");

		if (deviceId != null) {
			setDeviceId(deviceId);
		}

		String deviceToken = (String)attributes.get("deviceToken");

		if (deviceToken != null) {
			setDeviceToken(deviceToken);
		}

		String deviceName = (String)attributes.get("deviceName");

		if (deviceName != null) {
			setDeviceName(deviceName);
		}
	}

	@Override
	public long getDeviceId() {
		return _deviceId;
	}

	@Override
	public void setDeviceId(long deviceId) {
		_deviceId = deviceId;

		if (_deviceRemoteModel != null) {
			try {
				Class<?> clazz = _deviceRemoteModel.getClass();

				Method method = clazz.getMethod("setDeviceId", long.class);

				method.invoke(_deviceRemoteModel, deviceId);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public String getDeviceToken() {
		return _deviceToken;
	}

	@Override
	public void setDeviceToken(String deviceToken) {
		_deviceToken = deviceToken;

		if (_deviceRemoteModel != null) {
			try {
				Class<?> clazz = _deviceRemoteModel.getClass();

				Method method = clazz.getMethod("setDeviceToken", String.class);

				method.invoke(_deviceRemoteModel, deviceToken);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public String getDeviceName() {
		return _deviceName;
	}

	@Override
	public void setDeviceName(String deviceName) {
		_deviceName = deviceName;

		if (_deviceRemoteModel != null) {
			try {
				Class<?> clazz = _deviceRemoteModel.getClass();

				Method method = clazz.getMethod("setDeviceName", String.class);

				method.invoke(_deviceRemoteModel, deviceName);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	public BaseModel<?> getDeviceRemoteModel() {
		return _deviceRemoteModel;
	}

	public void setDeviceRemoteModel(BaseModel<?> deviceRemoteModel) {
		_deviceRemoteModel = deviceRemoteModel;
	}

	public Object invokeOnRemoteModel(String methodName,
		Class<?>[] parameterTypes, Object[] parameterValues)
		throws Exception {
		Object[] remoteParameterValues = new Object[parameterValues.length];

		for (int i = 0; i < parameterValues.length; i++) {
			if (parameterValues[i] != null) {
				remoteParameterValues[i] = ClpSerializer.translateInput(parameterValues[i]);
			}
		}

		Class<?> remoteModelClass = _deviceRemoteModel.getClass();

		ClassLoader remoteModelClassLoader = remoteModelClass.getClassLoader();

		Class<?>[] remoteParameterTypes = new Class[parameterTypes.length];

		for (int i = 0; i < parameterTypes.length; i++) {
			if (parameterTypes[i].isPrimitive()) {
				remoteParameterTypes[i] = parameterTypes[i];
			}
			else {
				String parameterTypeName = parameterTypes[i].getName();

				remoteParameterTypes[i] = remoteModelClassLoader.loadClass(parameterTypeName);
			}
		}

		Method method = remoteModelClass.getMethod(methodName,
				remoteParameterTypes);

		Object returnValue = method.invoke(_deviceRemoteModel,
				remoteParameterValues);

		if (returnValue != null) {
			returnValue = ClpSerializer.translateOutput(returnValue);
		}

		return returnValue;
	}

	@Override
	public void persist() throws SystemException {
		if (this.isNew()) {
			DeviceLocalServiceUtil.addDevice(this);
		}
		else {
			DeviceLocalServiceUtil.updateDevice(this);
		}
	}

	@Override
	public Device toEscapedModel() {
		return (Device)ProxyUtil.newProxyInstance(Device.class.getClassLoader(),
			new Class[] { Device.class }, new AutoEscapeBeanHandler(this));
	}

	@Override
	public Object clone() {
		DeviceClp clone = new DeviceClp();

		clone.setDeviceId(getDeviceId());
		clone.setDeviceToken(getDeviceToken());
		clone.setDeviceName(getDeviceName());

		return clone;
	}

	@Override
	public int compareTo(Device device) {
		long primaryKey = device.getPrimaryKey();

		if (getPrimaryKey() < primaryKey) {
			return -1;
		}
		else if (getPrimaryKey() > primaryKey) {
			return 1;
		}
		else {
			return 0;
		}
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}

		if (!(obj instanceof DeviceClp)) {
			return false;
		}

		DeviceClp device = (DeviceClp)obj;

		long primaryKey = device.getPrimaryKey();

		if (getPrimaryKey() == primaryKey) {
			return true;
		}
		else {
			return false;
		}
	}

	@Override
	public int hashCode() {
		return (int)getPrimaryKey();
	}

	@Override
	public String toString() {
		StringBundler sb = new StringBundler(7);

		sb.append("{deviceId=");
		sb.append(getDeviceId());
		sb.append(", deviceToken=");
		sb.append(getDeviceToken());
		sb.append(", deviceName=");
		sb.append(getDeviceName());
		sb.append("}");

		return sb.toString();
	}

	@Override
	public String toXmlString() {
		StringBundler sb = new StringBundler(13);

		sb.append("<model><model-name>");
		sb.append("pies.chat.model.Device");
		sb.append("</model-name>");

		sb.append(
			"<column><column-name>deviceId</column-name><column-value><![CDATA[");
		sb.append(getDeviceId());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>deviceToken</column-name><column-value><![CDATA[");
		sb.append(getDeviceToken());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>deviceName</column-name><column-value><![CDATA[");
		sb.append(getDeviceName());
		sb.append("]]></column-value></column>");

		sb.append("</model>");

		return sb.toString();
	}

	private long _deviceId;
	private String _deviceToken;
	private String _deviceName;
	private BaseModel<?> _deviceRemoteModel;
}